#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

vector<vector<int>> generateMatrix(int n) {
    int cur = 1;
    int l = 0, r = n - 1, up = 0, down = n - 1;
    vector<vector<int>> res(n, vector<int>(n));
    while (l <= r && up <= down)
    {
        for (int i = l; i <= r; i++)
            res[up][i] = cur++;
        for (int i = up + 1; i <= down; i++)
            res[i][r] = cur++;
        if (l < r&& up < down)
        {
            for (int i = r - 1; i > l; i--)
                res[down][i] = cur++;
            for (int i = down; i > up; i--)
                res[i][l] = cur++;
        }
        l++, r--, up++, down--;
    }
    return res;
}